package com.scvtc.exception;


import cn.afterturn.easypoi.exception.excel.ExcelExportException;
import com.scvtc.domain.ResResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

import static com.scvtc.domain.enums.AppHttpCodeEnum.*;

/**
 * @Description: 全局异常处理
 * @author: yiFei
 * @date: 2022/1/4 3:14
 */
@RestControllerAdvice
public class GlobalException {

    private static final Logger log = LoggerFactory.getLogger(GlobalException.class);
//     例如
//    @ExceptionHandler(错误的class)
//    @ResponseStatus(响应状态码)
//    private ResResult Exception(错误类 e, 请求 request) {
//
//        return ResResult.fail("");
//    }

    /**
     * 全局异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResResult<String> Exception(Exception e) {
        e.printStackTrace();
        log.error("未知错误 {}", e.getMessage());
        return ResResult.customize(SERVER_ERROR);
    }

    /**
     * 用户传入参数错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResResult<String> IllegalArgumentException(IllegalArgumentException e) {
        log.error("IllegalArgumentException 错误 {}", e.getMessage());
        return ResResult.self(FAIL.getCode(), e.getMessage());
    }

    /**
     * 权限不足
     *
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResResult<?> AccessDeniedException(AccessDeniedException e) {
        log.error("AccessDeniedException 错误 {}", e.getMessage());
        return ResResult.customize(INSUFFICIENT_PERMISSIONS);
    }

    /**
     * jsr303 校验
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResResult<?> ConstraintViolationException(ConstraintViolationException e) {
        String message = e.getMessage();
        log.error("ConstraintViolationException 错误 {}", message);
        int index = message.indexOf(':');
        return ResResult.self(FAIL.getCode(), message.substring(index == -1 ? 0 : index + 2));
    }

    /**
     * Excel导出错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ExcelExportException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResResult<?> ExcelExportException(ExcelExportException e) {
        String message = e.getMessage();
        log.error("ExcelExportException 错误 {}", message);
        return ResResult.self(FAIL.getCode(), message);
    }

    /**
     * 请求格式错误
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResResult<?> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        String message = e.getMessage();
        log.error("HttpRequestMethodNotSupportedException 错误 {}", message);
        return ResResult.self(FAIL.getCode(), message);
    }

    /**
     * 密码错误
     */
    @ExceptionHandler(BadCredentialsException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResResult<?> BadCredentialsException(BadCredentialsException e) {
        String message = e.getMessage();
        log.error("HttpRequestMethodNotSupportedException 错误 {}", message);
        return ResResult.customize(USER_NOT_FIND);
    }

    /**
     * 信息重复
     */
    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseStatus(HttpStatus.OK)
    public ResResult<?> DuplicateKeyException(DuplicateKeyException e) {
        String message = e.getMessage();
        log.error("HttpRequestMethodNotSupportedException 错误 {}", message);
        return ResResult.customize(DATA_REPEAT);
    }

}
